package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.enums.DUserOilCardStatusEnum;
import cn.huiyunche.base.service.interfaces.DUserOilCardService;
import cn.huiyunche.base.service.mappers.DUserOilcardMapper;
import cn.huiyunche.base.service.mappers.ext.DUserOilCardViewMapper;
import cn.huiyunche.base.service.model.DUserOilcard;
import cn.huiyunche.base.service.vo.DUserOilCardVo;
import cn.huiyunche.base.service.vo.PageVo;
import cn.huiyunche.base.service.vo.Result;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DUserOilCardServiceImpl implements DUserOilCardService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DUserOilCardServiceImpl.class);

    @Autowired
    private DUserOilcardMapper dUserOilcardMapper = null;

    @Autowired
    private DUserOilCardViewMapper dUserOilCardViewMapper = null;

    private DUserOilCardViewMapper getDUserOilCardViewMapper() {
        return this.dUserOilCardViewMapper;
    }

    private DUserOilcardMapper getDUserOilcardMapper() {
        return this.dUserOilcardMapper;
    }

    @Override
    public Result<Map<String, Object>> list(PageVo pageVo, DUserOilCardVo cardVo) throws Exception {
        LOGGER.info("list params pageVo: {}, cardVo: {}", pageVo, cardVo);
        Result<Map<String, Object>> result = new Result<>(true, null, "数据加载成功");
        pageVo.setTotalRecord(this.getDUserOilCardViewMapper().countByExample(cardVo));
        List<DUserOilCardVo> lists = this.getDUserOilCardViewMapper().selectByExample(pageVo, cardVo);
        Map<String, Object> map = new HashMap<>();
        if (CollectionUtils.isNotEmpty(lists)) {
            LOGGER.info("selectByExample reslut size: {}", lists.size());
            map.put("oilCard", lists);
        }
        map.putIfAbsent("oilCard", "");
        map.put("page", pageVo);
        result.setData(map);
        return result;
    }

    @Override
    public Result<Object> modifyStatus(Long id, String status) throws Exception {
        LOGGER.info("modifyStatus params id: {}, status: {}", id, status);
        Result<Object> result = new Result<>(true, null, "数据修改成功");
        if (null == id || id == 0) {
            LOGGER.info("id is not null");
            throw new BusinessException("ID不能为空");
        } else if (StringUtils.isBlank(status)) {
            LOGGER.info("status is not null");
            throw new BusinessException("油卡状态不能为空");
        } else if (StringUtils.isBlank(DUserOilCardStatusEnum.getByValue(status).getValue())) {
            LOGGER.info("oil status is error");
            throw new BusinessException("油卡状态错误");
        }
        DUserOilcard oilcard = new DUserOilcard();
        oilcard.setId(id);
        oilcard.setStatus(DUserOilCardStatusEnum.getByValue(status).getValue());
        result.setData(this.getDUserOilcardMapper().updateByPrimaryKeySelective(oilcard));
        return result;
    }

}
